package accountinfo.service;

import hackathon1212.ServletPojoAdapter;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import accountinfo.dao.Transaction;
import accountinfo.dao.TransactionsDAO;

public class Transactions extends ServletPojoAdapter {

	private static final long serialVersionUID = 1L;

	protected Object getBusinessMethod(Map<String, String[]> params) {
		// Query Parameters
		final String cardNO = params.get("cardNO")[0];
		final String cardsIN = params.get("cardsIN")[0];

		// Default response
		final Map<String, Object> ret = new HashMap<String, Object>();
		final List<Map<String, String>> transactions = new LinkedList<Map<String, String>>();
		ret.put("transactions", transactions);
		ret.put("status", "Card Not Found");

		String cardSearch = cardNO;
		// Get Account Number (11 digits) if requestor wants all cards)
		if( cardsIN == "ALL" ){cardSearch = cardNO.substring(0, 11);}
		
		// Search for transactions with the same first 11 digits of card number
		final List<Transaction> items = TransactionsDAO.getTransactionDetails(cardSearch);
		
		if (!items.isEmpty())
		{
			for (Transaction item : items) {
				final Map<String, String> transaction = new HashMap<String, String>();
				transaction.put("referenceNO", item.getReferenceNO());
				transaction.put("monetaryTypeCD", item.getMonetaryTypeCD());
				transaction.put("billedUnBilled", item.getBilledUnBilled());
				transaction.put("statementDT", item.getStatementDT());
				transaction.put("cardNO", item.getCardNO());
				transaction.put("description", item.getDescription());
				transaction.put("postedDT", item.getPostedDT());
				transaction.put("processDT", item.getProcessDT());
				transaction.put("amount", item.getAmount());
				transaction.put("spendType", item.getSpendType());
				transactions.add(transaction);
			}
			ret.put("status", "Success");
		}		
		return ret;
	}
}
